<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;

class Kemu extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }
    public function kemu()
    {
        return $this->view->fetch();
    }
    public function indexold()
    {
        return $this->view->fetch();
    }
	//--------原有用户数据转化-------
	public function adduser(){
		$page = isset($_GET['page']) ? $_GET['page'] : 1;
		$url = 'http://vip.kaoguo8.com/portal/userapi/index?limit=20&page='.$page;
		 $curl=curl_init();
		curl_setopt($curl, CURLOPT_URL,$url);
		// curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		// curl_setopt($curl, CURLOPT_POST, true);  //设置请求为post
		// curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);  //post的数据内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ; //请求执行时，不将响应数据直接输出，而是以返回值的形式输出响应数据
		curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
		$res=curl_exec($curl);
		curl_close($curl);
		
		$array = json_decode($res,true);
		var_dump($array);
		if($array){
			foreach ($array['data'] as $key =>$val){
				$add['id'] = $val['id'];
				$add['group_id'] = 0;
				$add['username'] = $val['mobile'];
				$add['nickname'] = $val['mobile'] ? substr($val['mobile'],0,3).'****'.substr($val['mobile'],-4) : '';
				$add['password'] = trim($val['user_pass'],'###');
				$add['salt'] = '31yROlcjKKeHeLgOao';
				$add['email'] = '';
				$add['mobile'] = $val['mobile'];
				$add['avatar'] = $val['avatar'];
				$add['level'] = 0;
				$add['gender'] = 0;
				$add['joinip'] = $val['last_login_ip'];//登录IP
				$add['loginip'] = $val['last_login_ip'];
				$add['logintime'] = $val['last_login_time'];//登录时间
				$add['prevtime'] = $val['last_login_time'];//上次登陆时间
				$add['createtime'] = $val['create_time'];//创建时间
				// $add['birthday'] = 0;
				$add['bio'] = '';//格言
				$add['status'] = 'normal';//状态
				$add['entry_platform'] = $val['entry_platform'];//平台来源
				$add['url_source'] = $val['url_source'];//地址来源
				$add['class_name'] = $val['class_name'];//专业名称 英文
				$add['class_title'] = $val['class_title'];//	专业名称 中文	
				$add['use_info'] = json_encode($val);
				Db::name('user-')->insert($add);
			}
		}
		
		if($page < $array['last_page']){
			$next =$page + 1;
		}else{
			echo "ok";exit;
		}
$url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/index/index/adduser?page='.$next;
//        echo '<meta http-equiv="refresh" content="2;url='.$url.'>';
echo "<script language='JavaScript'>
            setTimeout(function(){window.location = '{$url}'},1000); //指定3秒刷新一次
        </script>";

	}

    //--------原有订单数据转化-------
	public function addorder(){
		$page = isset($_GET['page']) ? $_GET['page'] : 1;
		$url = 'http://vip.kaoguo8.com/portal/userapi/order?limit=100&page='.$page;
		 $curl=curl_init();
		curl_setopt($curl, CURLOPT_URL,$url);
		// curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		// curl_setopt($curl, CURLOPT_POST, true);  //设置请求为post
		// curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);  //post的数据内容
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ; //请求执行时，不将响应数据直接输出，而是以返回值的形式输出响应数据
		curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
		$res=curl_exec($curl);
		curl_close($curl);
		
		$array = json_decode($res,true);
		var_dump($array);
		if($array){
			foreach ($array['data'] as $key =>$val){
				$add['id'] = $val['id'];//id
				$add['order_no'] = $val['pay_code'];//订单号
				$add['title'] = $val['pay_info'];//名称
				$add['sale_en_name'] = '';//科目英文
				$add['sale_product_id'] = '';//产品id
				$add['total_amount'] = $val['totalfee'];//合计分
				$add['reduce_amount'] = $val['youhui'];//优惠分
				$add['actual_amount'] = $val['shifu'];//实付分
				$add['cdk'] = $val['sn'];//激活码
				$add['mobile'] =  $val['mobile'];//手机号
				$add['user_id'] = $val['userid'];//用户id
				$add['paytype'] = $val['paybank'];//支付方式
				$add['paymethod'] = $val['paybank'];//支付方法
				$add['paystatus'] = $val['status'];//支付状态
				$add['source'] = '';//来源
				$add['kf'] = $val['kefu'];//客服归属
				$add['remark'] = $val['beizhu'];//备注
				$add['createtime'] = $val['buytime'];//下单时间
				$add['updatetime'] = $val['buytime'];//更新时间
				$add['sendtime'] = $val['sendtime'];//更新时间
				$add['deletetime'] = '';//删除时间
				Db::name('xorder0')->insert($add);
			}
		}
		
		if($page < $array['last_page']){
			$next =$page + 1;
		}else{
			echo "ok";exit;
		}
$url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/index/index/addorder?page='.$next;
//        echo '<meta http-equiv="refresh" content="2;url='.$url.'>';
echo "<script language='JavaScript'>
            setTimeout(function(){window.location = '{$url}'},800); //指定3秒刷新一次
        </script>";

	}

}
